package com.yzg.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzg.pojo.ProductAttributeCategory;
import com.yzg.service.ProductAttributeCategoryService;
import com.yzg.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: decade
 * @Description: 商品属性分类控制层
 * @Date: 2021/1/12 13:37
 **/
@Api(tags = "ProductAttributeCategoryController", description = "商品属性分类管理")
@RestController
@RequestMapping("/pms/productAttrCate")
@CrossOrigin
public class ProductAttributeCategoryController {
    @Autowired
    private ProductAttributeCategoryService productAttributeCategoryService;

    @GetMapping("/getTreeList")
    @ApiOperation(value = "获取商品属性分类及其子属性")
    public R getTreeList(){
        List<ProductAttributeCategory> list = productAttributeCategoryService.findAttrCateALl();
        return R.ok().data("list",list);
    }

    @GetMapping("/list/{page}/{pageSize}")
    @ApiOperation(value = "获取商品属性分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,paramType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "当前页大小",required = true,paramType = "Integer")
    })
    public R getList(@PathVariable Integer page,@PathVariable Integer pageSize){
        IPage<ProductAttributeCategory> pageParam = new Page<>(page,pageSize);
        List<ProductAttributeCategory> list = productAttributeCategoryService.getList(pageParam);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("total",pageParam.getTotal());
        return R.ok().data(map);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取商品属性分类")
    @ApiImplicitParam(name = "id",value = "商品属性分类id",required = true,paramType = "Long")
    public R getProductAttrCateById(@PathVariable Long id){
       ProductAttributeCategory productAttrCate = productAttributeCategoryService.getProductAttrCateById(id);
        return R.ok().data("productAttrCate",productAttrCate);
    }

    @PutMapping("/update")
    @ApiOperation(value = "商品属性分类编辑")
    @ApiImplicitParam(name = "productAttrcate",value = "商品属性分类信息",required = true,paramType = "ProductAttributeCategory")
    public R update(@RequestBody ProductAttributeCategory productAttrcate){
        int row = productAttributeCategoryService.update(productAttrcate);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @PostMapping("/add")
    @ApiOperation(value = "商品属性分类添加")
    @ApiImplicitParam(name = "productAttrcate",value = "商品属性分类信息",required = true,paramType = "ProductAttributeCategory")
    public R add(@RequestBody ProductAttributeCategory productAttrcate){
        int row = productAttributeCategoryService.addProductAttrCate(productAttrcate);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "商品属性分类删除")
    @ApiImplicitParam(name = "id",value = "商品属性分类id",required = true,paramType = "Long")
    public R delete(@PathVariable Long id){
        int row = productAttributeCategoryService.delete(id);
        if(row>0){
            return R.ok();
        }else{
            return R.error();
        }
    }
}
